package top.malegod.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import top.malegod.model.User;
import top.malegod.service.CommunityService;
import top.malegod.utils.ServerResponse;

/**
 * Create by IntelliJ IDEA
 *
 * @author: AbelEthan
 * @date-Time: 2018/5/24 15:38
 * @description: 社区的基本功能
 */
@Controller
@RequestMapping("/community")
public class CommunityController {

  @Autowired
  private CommunityService communityService;

  /**
   * 获取社区信息
   * @return
   */
  @RequestMapping("/findAll.do")
  @ResponseBody
  public ServerResponse findAll() {

    ServerResponse response = communityService.findAll();
    return response;
  }

  /**
   * 获取单个信息
   * @param id
   * @return
   */
  @RequestMapping("/findSingle.do")
  @ResponseBody
  public ServerResponse findSingle(String id) {
    ServerResponse response = communityService.findSingle(id);
    return response;
  }

  /**
   * 获取个人社区
   * @param session
   * @return
   */
  @RequestMapping("/findUserCommunity.do")
  @ResponseBody
  public ServerResponse findUserCommunity(HttpSession session) {
    User user = (User) session.getAttribute("user");
    if (user == null) {
      return ServerResponse.createError("你还没登陆，请先登录");
    }
    ServerResponse response = communityService.findUserCommunity(user.getId());
    return response;
  }

  /**
   * 用户增加社区
   * @param file
   * @param content
   * @param request
   * @return
   * @throws IOException
   */
  @RequestMapping(value = "/addCommunity.do", method = RequestMethod.POST)
  @ResponseBody
  public ServerResponse addCommunity(
      MultipartFile file,
      @RequestParam(value = "content") String content,
      HttpServletRequest request) throws IOException {
    User user = (User) request.getSession().getAttribute("user");
    if (user == null) {
      return ServerResponse.createError("你还没登录，请先登录");
    }

    ServerResponse response = communityService.addCommunity(file,content,user.getId(),request);
    return response;
  }


  /**
   * 删除社区
   * @param id
   * @param session
   * @return
   */
  @RequestMapping("/deleteCommunity.do")
  @ResponseBody
  public ServerResponse deleteCommunity(String id,
      HttpSession session) {
    User user = (User) session.getAttribute("user");
    if (user == null) {
      return ServerResponse.createError("你还没登录，请先登录");
    }
    ServerResponse response = communityService.deleteCommunity(id);
    return response;
  }

  /**
   * 社区搜索
   * @param content
   * @return
   */
  @RequestMapping("/searchCommunity.do")
  @ResponseBody
  public ServerResponse searchCommunity(String content){
    ServerResponse response = communityService.searchCommunity(content);
    return response;
  }

}
